/**
 * 读取配置文件信息
 */
package com.ydd.common.util;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 读取属性文件的Class类
 * 
 * @author TONG
 * 
 */
public class MsgUtils {
	
	// log出力的初始化
	private static final Log log = LogFactory.getLog(MsgUtils.class);
	private Properties p;

	// 读取MSG属性文件的构造函数
	public MsgUtils() {
		// 读取message.properties属性文件
		BufferedInputStream msgStream = (BufferedInputStream) this.getClass().getResourceAsStream("/message.properties");
		BufferedInputStream sysStream = (BufferedInputStream) this.getClass().getResourceAsStream("/system.properties");
//		
//		ByteArrayInputStream msgStream = (ByteArrayInputStream) this.getClass().getResourceAsStream("/message.properties");
//		ByteArrayInputStream sysStream = (ByteArrayInputStream) this.getClass().getResourceAsStream("/system.properties");
		try {
			
			Vector<InputStream> vector = new Vector<InputStream>();  
            vector.addElement(msgStream);  
            vector.addElement(sysStream);  
            Enumeration<InputStream> e = vector.elements();  
            SequenceInputStream inStream = new SequenceInputStream(e);  

			p = new Properties();
			p.load(inStream);
			
			msgStream.close();
			sysStream.close();
			inStream.close();
		} catch (FileNotFoundException e) {
			log.error(e);
		} catch (IOException e) {
			log.error(e);
		}
	}

	/**
	 * 取得MSG信息
	 * @param itemName 取得MSG的Key
	 * @return 结果MSG
	 */
	public String getValue(String itemName) {
		return p.getProperty(itemName);
	}

	/**
	 * 取得MSG信息
	 * @param itemName 取得MSG-Key
	 * @param defaultValue 取得MSG-Key
	 * @return 结果MSG
	 */
	public String getValue(String itemName, Object[] fixStr) {
		return MessageFormat.format(p.getProperty(itemName), fixStr);
	}
	
	public static void main(String[] args) {
		 new MsgUtils();
	}

}
